﻿using DataModels.Dto;
using DataModels.Dto.WorkOrder;
using DataModels.Entities.Base;
using DataModels.Entities.House;
using DataModels.Entities.Visitor;
using DataModels.Entities.WorkOrder;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Dm.net.buffer.ByteArrayBuffer;

namespace Services.Feedback
{
    public class FeedbackService(SqlSugarClient db) : BaseService(db)
    {
        public ResponseModel AddFeedback(DataModels.Entities.Feedback.Feedback feedback)
        {
            // 自动生成 UUID 和创建时间
            feedback.UUID = Guid.NewGuid().ToString();
            feedback.UserUUID = UserUUID.ToString();
            feedback.CreateTime = DateTime.Now;
            var result = db.Insertable(feedback).ExecuteCommand();
            return result > 0 ? Success() : Error("创建失败");
        }



        //门禁

        /// <summary>
        /// 门禁列表
        /// </summary>
        /// <param name="ProjectUUID"></param>
        /// <returns></returns>
        public ResponseModel GetAccessControlList(OpenDoorRequest o)
        {
            if (o.ACid > 0)
            {
                return Success(db.Queryable<AccessControl>().Where(ac => ac.Id == o.ACid).First());
            }
            List<AccessControl> acl = db.Queryable<AccessControl, Projects, Building, HousingBasic>((ac, p, bu, h) => new object[]
            {
                JoinType.Left,ac.ProjectUUID == p.UUID,
                JoinType.Left,ac.BuildingUUID == bu.UUID,
                JoinType.Left,bu.BuildId == h.BuildId
            })
                .Where(ac => ac.ProjectUUID == o.ProjectUUID)
                //.Where((ac, p, bu, h) => h.UUID== HouseUUID)

                .ToList()
                ;
            return Success(acl);
        }
    }

    public class OpenDoorRequest
    {
        public Guid ProjectUUID { get; set; }
        public int ACid { get; set; }
        public int saomaid { get; set; }
    }
}
